/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.Category;
import entity.News;
import entity.SubCategory;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.CategoryModel;
import model.NewsModel;
import model.SubCategoryModel;

/**
 * 
 * @author xuanhung2401
 */
@SuppressWarnings("serial")
public class NewsSubCategory extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String url = request.getRequestURI();
		String[] splitUrl = url.split("/");		
		System.out.println("from sub");
		String subAlias = splitUrl[splitUrl.length - 1];
		String cateAlias = splitUrl[splitUrl.length - 2];;
		NewsModel newsModel = new NewsModel();
		CategoryModel cateModel = new CategoryModel();
		SubCategoryModel subModel = new SubCategoryModel();
		SubCategory sub = subModel.findByAlias(subAlias);
		Category cate = cateModel.findByAlias(cateAlias);		
		if (sub != null && !sub.getAlias().equalsIgnoreCase("")) {		
			List<News> list = newsModel.findBySubCategory(cateAlias, subAlias, 11);
			request.setAttribute("list", list);
			request.setAttribute("cate", cate);
			request.setAttribute("sub", sub);
			request.getRequestDispatcher("/sub_detail.jsp").forward(request,
					response);
		} else {
		}
	}
}
